package com.xin.di.uav.applet.controller;

import com.xin.di.uav.applet.resp.OrderResp;
import com.xin.di.uav.applet.service.OrderService;
import com.xin.di.uav.common.annotate.DistributedLock;
import com.xin.di.uav.common.annotate.ReSubmit;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.utils.RedisKeyConstants;
import com.xin.di.uav.common.utils.Rs;
import com.xin.di.uav.user.order.service.CommonOrderService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;


@RestController
@RequestMapping("/home/order/")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private CommonOrderService commonOrderService;


//    @DeleteMapping("{id}")
//    public Rs<Boolean> delete(@PathVariable String id) {
//        return Rs.success(serviceAreaService.delete(id));
//    }


    @PutMapping("update-status")
    @ReSubmit(key = RedisKeyConstants.updateOrderStatus, expire = 3)
    @DistributedLock(key = RedisKeyConstants.updateOrderStatus, expireTime = 5, userDimension = true)
    public Rs<Integer> updateStatus(@RequestBody @Valid IdRequest idRequest) {
        return Rs.success(orderService.updateStatus(idRequest));
    }

//    @GetMapping("{id}")
//    public Rs<ServiceAreaPO> getById(@PathVariable String id) {
//        return Rs.success(serviceAreaService.getById(id));
//    }

    @GetMapping("list")
    public Rs<OrderResp> list(@RequestParam int page, @RequestParam int pageSize, @RequestParam(required = false) String orderNo, @RequestParam(required = false) String payPhone) {
        return Rs.success(orderService.list(page, pageSize, orderNo, payPhone));
    }

    @PostMapping("cancel-order")
    @Operation(tags = "取消订单", description = "取消订单")
    @CrossOrigin
    @ReSubmit(key = RedisKeyConstants.cancelOrder, expire = 3)
    @DistributedLock(key = RedisKeyConstants.cancelOrder, expireTime = 5, userDimension = true)
    public Rs<Date> cancelOrder(@RequestBody @javax.validation.Valid IdRequest idRequest) {
        return Rs.success(commonOrderService.cancelOrder(idRequest.getId()));
    }

}
